<?php
import("ORG.Util.Function");
import("@.Org.Utils");
import("@.Org.PackTool");


class PackAction extends CommonAction{	
	
	public function _initialize(){
		parent::_initialize();				
		$param           =    $this->_get('_URL_');
		$controller      =    $param[0];
		$action          =    $param[1];		
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
	
	
	/**
	 * 包装列表
	 * Enter description here ...
	 */
	public function pack_list(){
	
	    $this->assign('packs_list',   $packs_list['packs_list']);
	    $this->assign('filter',       $packs_list['filter']);
	    $this->assign('record_count', $packs_list['record_count']);
	    $this->assign('page_count',   $packs_list['page_count']);
	    
	    // 包装列表SQL
	    $sql_packs_list = get_sql_packs_list();
	    
	    $param			=	array(
	    	'result'	=> $sql_packs_list,
	    	'listvar'	=> 'packs_list',
	    	'listRows'	=> 20,
	    	'parameter'	=> '',
	    	'target'	=> 'content',
	    	'pagesId'	=> 'page',
	    	'template'	=> 'Pack:pack_list',
	    );
	    
	    $this->page_pack_list($param);	    
		$this->display();
	}
	
	/**
	 * 包装列表分页
	 * Enter description here ...
	 */
	public function page_pack_list($param){
		extract($param);
		//总记录数
		$flag = is_string($result);
		
		$listvar = $listvar ? $listvar : 'list';
		$listRows = $listRows? $listRows : 21;
		if ($flag){
			$totalRows	=	count(M()->query($result));
		}			
		else{
			$totalRows = ($result) ? count($result) : 1;
		}
		//创建分页对象
		if ($target && $pagesId)
			$p = new Page($totalRows, $listRows, $parameter, $url,$target, $pagesId);
		else
			$p = new Page($totalRows, $listRows, $parameter,$url);
		//抽取数据
		if ($flag) {
			$result .= " LIMIT {$p->firstRow},{$p->listRows}";
			$voList = M()->query($result);
		} else {
			$voList = array_slice($result, $p->firstRow, $p->listRows);
		}
		$pages = C('PAGE');//要ajax分页配置PAGE中必须theme带%ajax%，其他字符串替换统一在配置文件中设置，
		
		//可以使用该方法前用C临时改变配置
		foreach ($pages as $key => $value) {
			$p->setConfig($key, $value); // 'theme'=>'%upPage% %linkPage% %downPage% %ajax%'; 要带 %ajax%
		}
		//分页显示
		$page = $p->show();		
		//模板赋值
		$this->assign($listvar, $voList);
		$this->assign("page", $page);
		if ($this->isAjax()) {//判断ajax请求
			layout(false);
			$template = (!$template) ? 'ajaxlist' : $template;
			exit($this->fetch($template));
		}
		return $voList;
	}
		
	/**
	 * 添加包装
	 * Enter description here ...
	 */
	public function add_pack(){
	
	    $pack['pack_fee'] 		= 	0;
	    $pack['free_money'] 	= 	0;
	
	    $this->assign('pack',         $pack);
	    $this->assign('form_action','insert');
	    $this->assign('action',		'add_pack_ok');
		
		$this->display('pack_info');
	}
	
	/**
	 * 处理添加包装
	 * Enter description here ...
	 */
	public function add_pack_ok(){
		
		import("@.Plugs.Exchange");
		
		$exc 			= 	new Exchange('ybr_pack', $db, 'pack_id', 'pack_name');
		
	    /*检查包装名是否重复*/
	    $is_only 		= 	$exc->is_only('pack_name', $_POST['pack_name']);
	
	    if (!$is_only){
	        //sys_msg(sprintf($_LANG['packname_exist'], stripslashes($_POST['pack_name'])), 1);
	        $this->error( sprintf(L('packname_exist'), stripslashes($_POST['pack_name'])) );
	    }
	
	    $img_name		=	'';
	    /* 处理图片 */
	    if (!empty($_FILES['pack_img'])){
		    import("ORG.Net.UploadFile");
			//导入上传类
			$upload 			= 	new UploadFile();
			//设置上传文件大小
			$upload->maxSize 	= 	3292200;
			//设置上传文件类型
			$upload->allowExts 	= 	explode(',','jpg,gif,png,jpeg');
			//设置附件上传目录
			$upload->savePath 	= 	'./Public/Upload/images/';
						
			if (!$upload->upload()) {
			    //捕获上传异常
			    //$this->error($upload->getErrorMsg());
			} else {
			    //取得成功上传的文件信息
			    $uploadList 	= 	$upload->getUploadFileInfo();
			    $img_name		=	$uploadList[0]['savename'];
			}
	    }
	
	    /*插入数据*/
	    $sql 					= 	"INSERT INTO " . 'ybr_pack' . "(pack_name, pack_fee, free_money, pack_desc, pack_img)
	            					VALUES ('$_POST[pack_name]', '$_POST[pack_fee]', '$_POST[free_money]', '$_POST[pack_desc]', '$img_name')";
	    M()->query($sql);

	    admin_log($_POST['pack_name'],'add','pack');// 记录添加的包装
	    
		$this->success("操作成功",U('pack/add_pack'));
	}
	
	
	/**
	 * 修改包装信息
	 * 
	 */
	public function mod_pack(){
		$CommonModel	=	new CommonModel();
	
	    $sql 			= 	"SELECT pack_id, pack_name, pack_fee, free_money, pack_desc, pack_img FROM " . ' ybr_pack ' .  " WHERE pack_id='$_REQUEST[id]'";
	    $pack 			= 	$CommonModel->getRow($sql);
	    
	    $this->assign('pack',       $pack);
	    $this->assign('form_action','update');	    
	    $this->assign('action','mod_pack_ok');	    
		$this->display('pack_info');
	}
	
	/**
	 * 处理修改包装
	 * Enter description here ...
	 */
	public function mod_pack_ok(){
		
		import("@.Plugs.Exchange");		
		$exc 			= 	new Exchange('ybr_pack', $db, 'pack_id', 'pack_name');
		
		
	    if ($_POST['pack_name'] != $_POST['old_packname']){
	        /*检查品牌名是否相同*/
	        $is_only 	= 	$exc->is_only('pack_name', $_POST['pack_name'], $_POST['id']);	
	        if (!$is_only){
	            //sys_msg(sprintf($_LANG['packname_exist'], stripslashes($_POST['pack_name'])), 1);
	            $this->error( sprintf(L('packname_exist'), stripslashes($_POST['pack_name'])) );
	        }
	    }
	
	    $param 			= 	"pack_name = '$_POST[pack_name]', pack_fee = '$_POST[pack_fee]', free_money= '$_POST[free_money]', pack_desc = '$_POST[pack_desc]' ";
	    
	    $img_name		=	'';
	    /* 处理图片 */
	    if (!empty($_FILES['pack_img']['name'])){	    	
	    	import("ORG.Net.UploadFile");
			//导入上传类
			$upload 			= 	new UploadFile();
			//设置上传文件大小
			$upload->maxSize 	= 	3292200;
			//设置上传文件类型
			$upload->allowExts 	= 	explode(',','jpg,gif,png,jpeg');
			//设置附件上传目录
			$upload->savePath 	= 	'./Public/Upload/images/';
						
			if (!$upload->upload()) {
			    //捕获上传异常
			    //$this->error($upload->getErrorMsg());
			} else {
			    //取得成功上传的文件信息
			    $uploadList 	= 	$upload->getUploadFileInfo();
			    $img_name		=	$uploadList[0]['savename'];
			}	    	
	    }
	    else{
	        $img_name 			= 	'';
	    }
	
	    if (!empty($img_name)){
	        $param 				.= 	" ,pack_img = '$img_name' ";
	    }
	
	    if ($exc->edit($param ,  $_POST['id'])){
	        $note 				= 	sprintf( L('packedit_succed'), $_POST['pack_name']);	        
	        admin_log($_POST['pack_name'], 'edit', 'pack');	// 记录操作	        
	        $this->success($note,U('pack/pack_list'));
	    }
	    else{
	        //die($db->error());
	        $this->error("操作失败");	        
	    }		
	}
		
	/**
	 * 移除包装
	 * Enter description here ...
	 */
	public function remove_pack(){
		
		import("@.Plugs.Exchange");		
		$exc 			= 	new Exchange('ybr_pack', $db, 'pack_id', 'pack_name');
		
		$dir		=	"./Public/Upload/images/";
		
	    $id     	= 	intval($_GET['id']);
	    $name   	= 	$exc->get_name($id);
	    $img    	= 	$exc->get_name($id, 'pack_img');
	
	    if ( is_array($exc->drop($id)) || $exc->drop($id) ){
	        /* 删除图片 */
	        if (!empty($img)){
	             @unlink($dir . $img);
	        }
	        admin_log(addslashes($name),'remove','pack');	
			$this->success("操作成功",U('pack/pack_list'));
	    }
	    else{
	        $this->error(L('packremove_falure'));
	        return false;
	    }		
	}
	
	
	
	
	
	
	
	
	
}












